using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Com.queryzer2.Domain;
using common.pages;
using System.Collections.Generic;
using Com.queryzer2.Domain.Entities;


namespace Com.AlbLab.CR.Web
{
    public partial class UtenteMasterDetails : System.Web.UI.UserControl
    {

        #region Events and Delegates

    
        #endregion
        #region Public Properties
        
       /* private string azId;
        public string AzId
        {
            get { return azId; }
            set { this.azId = value; }
        }
        */
        
        public bool ForceEditsInFormView
        {
            get { return Convert.ToBoolean(ViewState["ForceEditsInFormView"]); }
            set { ViewState["ForceEditsInFormView"] = true; }
        }
        public bool AllowGridViewRecordSorting
        {
            get { return gvOrders.AllowSorting; }
            set { gvOrders.AllowSorting = value; }
        }
        public bool AllowGridViewRecordSelection
        {
            get { return gvOrders.AutoGenerateSelectButton; }
            set { gvOrders.AutoGenerateSelectButton = value; }
        }

        public bool AllowFormViewRecordInserting
        {
            get { return Convert.ToBoolean(ViewState["AllowFormViewRecordInserting"]); }
            set { ViewState["AllowFormViewRecordInserting"] = value; }
        }
        public bool AllowFormViewRecordEditing
        {
            get { return Convert.ToBoolean(ViewState["AllowFormViewRecordEditing"]); }
            set { ViewState["AllowFormViewRecordEditing"] = value; }
        }
        public bool AllowFormViewRecordDeleting
        {
            get { return Convert.ToBoolean(ViewState["AllowFormViewRecordDeleting"]); }
            set { ViewState["AllowFormViewRecordDeleting"] = value; }
        }
        #endregion

        //FormView DataBound Item
        public UtenteEntity e0;
        protected enum ViewMode { Unknown, GridView, FormView }
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        public void Initialize()
        {
            AllowGridViewRecordSorting = true;
            AllowGridViewRecordSelection = true;

            AllowFormViewRecordDeleting = true;
            AllowFormViewRecordEditing = true;
            AllowFormViewRecordInserting = true;

            ForceEditsInFormView = false;

            BindDataViews(0);
        }

        protected void BindDataViews(int _OrderID)
        {
            try
            {
                Dictionary<decimal, UtenteEntity> data = BasePage.Services.UtenteService.getListaUtenti();

                if (data != null && data.Count > 0)
                {
                    gvOrders.DataSource = data.Values;
                    gvOrders.DataBind();
                }
                else
                {
                    gvOrders.DataSource = null;
                    gvOrders.DataBind();
                }

              
                if (_OrderID > 0)
                {

                    e0 = BasePage.Services.UtenteService.getUtenteByID(_OrderID);
                    fvOrders.DataBind();

                    return;
                }
                if (e0 != null)
                {
                    fvOrders.DataBind();
                }
            }
            catch (ManagedException e0x)
            {
                ((BasePage)this.Page).info.AddMessage(e0x);
            }
        }


        #region GridView Events

        protected void gvOrders_RowDataBound(object sender, GridViewRowEventArgs e)
        {
           
        }

        protected void gvOrders_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (gvOrders.SelectedIndex < 0)
                return;

            fvOrders.ChangeMode(FormViewMode.ReadOnly);

            int _OrderID = int.Parse(((Label)gvOrders.SelectedRow.Cells[1].Controls[1]).Text);

            BindDataViews(_OrderID);

        }

        protected void gvOrders_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvOrders.PageIndex = e.NewPageIndex;
            BindDataViews(0);
        }

        #endregion
        protected void lbtnFormNewOrders_Click(object sender, EventArgs e)
        {
            fvOrders.ChangeMode(FormViewMode.Insert);
            fvOrders.DataSource = null;
            fvOrders.DataBind();
        }

        protected void lbtnFormEditOrders_Click(object sender, EventArgs e)
        {
            fvOrders.ChangeMode(FormViewMode.Edit);
            int _OrderID = int.Parse(((Label)fvOrders.FindControl("lblId")).Text);
            BindDataViews(_OrderID);
        }

        protected void lbtnFormDeleteOrders_Click(object sender, EventArgs e)
        {
            fvOrders.DeleteItem();
            fvOrders.ChangeMode(FormViewMode.Insert);
            BindDataViews(0);
        }

        protected void lbtnFormInsertOrders_Click(object sender, EventArgs e)
        {
            fvOrders.InsertItem(false);
            fvOrders.ChangeMode(FormViewMode.ReadOnly);
            BindDataViews(0);
        }

        protected void lbtnFormCancelUpdateOrders_Click(object sender, EventArgs e)
        {
            fvOrders.ChangeMode(FormViewMode.ReadOnly);
            int _OrderID = int.Parse(((Label)fvOrders.FindControl("lblId")).Text);
            BindDataViews(_OrderID);
        }

        protected void lbtnFormUpdateOrders_Click(object sender, EventArgs e)
        {
            fvOrders.UpdateItem(false);
            fvOrders.ChangeMode(FormViewMode.ReadOnly);
            BindDataViews(0);
        }

        #region "FormView LifeCycle"
        protected void fvOrders_ItemDeleting(object sender, FormViewDeleteEventArgs e)
        {
            try
            {
                string id = ((Label)fvOrders.FindControl("lblId")).Text;
                //((BasePage)this.Page).ServiceUtente().deleteUtente(decimal.Parse(id));
                e.Cancel = true;
            }
            catch (ManagedException e0x)
            {
                ((BasePage)this.Page).info.AddMessage(e0x);
            }
        }

        protected void fvOrders_ItemInserting(object sender, FormViewInsertEventArgs e)
        {
            
        }

        protected void fvOrders_ItemUpdating(object sender, FormViewUpdateEventArgs e)
        {
            float x;
            foreach (DictionaryEntry entry in e.NewValues)
            {
                if (entry.Key.ToString() == "Valore")
                {

                    //entry.Value.ToString().Replace(",", ".");
                    e.NewValues[entry.Key] = entry.Value.ToString().Replace(",", ".");
                }
            }
        }

        protected void fvOrders_ModeChanging(object sender, FormViewModeEventArgs e)
        {
            //int _OrderID = int.Parse(((Label)gvOrders.SelectedRow.Cells[1].Controls[1]).Text);
            if (e.CancelingEdit)
            {
                BindDataViews(0);
            }
        }

        protected void fvOrders_DataBound(object sender, EventArgs e)
        {
            if (fvOrders.CurrentMode == FormViewMode.Insert)
            {
                ((CheckBox)fvOrders.FindControl("cbUser")).Checked = true;
            }
        }

        #endregion

        protected void ODS_Updating(object sender, ObjectDataSourceMethodEventArgs e)
        {
            try
            {
                e0 = BasePage.Services.UtenteService.updateUtente((UtenteEntity)e.InputParameters[0]);
                e.Cancel = true;
            }
            catch (ManagedException e0x)
            {
                ((BasePage)this.Page).info.AddMessage(e0x);
            }
        }
        protected void ODS_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance = e0;
        }
        protected void ODS_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            try
            {
                e0 = BasePage.Services.UtenteService.insertUtente((UtenteEntity)e.InputParameters[0]);
                e.Cancel = true;
            }
            catch (ManagedException e0x)
            {
                ((BasePage)this.Page).info.AddMessage(e0x);
            }
        }

        protected void gvOrders_RowCommand(object sender, GridViewCommandEventArgs e)
        {

        }
    }

}